//
//  MYTabBarController.swift
//  NewMicroblog
//
//  Created by apple on 2016/10/22.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
import SVProgressHUD
class MYTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
       addChild()
        
        let tabBar = MYTabBar()
        tabBar.closure = {
            if !MYUserinfoViewModel.sharedModel.islogin {
                SVProgressHUD.showError(withStatus: "请先登录，亲~")
                return
            }
            
            let composeView = MYComposeView()
            composeView.show(target: self)
        }
        
        setValue(tabBar, forKey: "tabBar")
    }
    
    /// 添加子控制器
    func addChild(){
        
        addChildViewController(MYHomeViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MYMessageViewController(), title: "消息", imageName: "tabbar_message_center")
        
        addChildViewController(MYDiscoverViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(MYProfileViewController(), title: "我的", imageName: "tabbar_profile")
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
       
    }
    
    /// 添加导航控制器
    ///
    /// - parameter childController: 子控制器
    /// - parameter title:           名称
    /// - parameter imageName:       图片
    private func addChildViewController(_ childController: UIViewController, title : String, imageName : String) {
        
        childController.title = title
        
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orange], for: UIControlState.selected)
        childController.tabBarItem.image = UIImage(named:imageName)
        childController.tabBarItem.selectedImage = UIImage(named:"\(imageName)_selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        let nav = MYNavigationController(rootViewController: childController)
        addChildViewController(nav)
        
    }


}
